<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control class="tab-control"
                 :titles="['流行','新款','精选']"
                 @tabClick='tabClick'
                 ref="tabControl1"
                 v-show="isTabFixed" />
    <scroll class="content"
            ref="scroll"
            :probe-type='3'
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp='loadMore'>
      <home-swiper :banners="banners"
                   @swiperImageLoad='swiperImageLoad'>
      </home-swiper>
      <recommend-view :recommends='recommends' />
      <feature-view />
      <tab-control :titles="['流行','新款','精选']"
                   @tabClick='tabClick'
                   ref="tabControl2" />
      <goods-list :goods="showGoods" />
    </scroll>
    <back-top @click.native="backClick"
              v-show="isShowBackTop"></back-top>

  </div>
</template>

<script>
import { debounce } from 'common/untils.js'
import NavBar from 'components/common/navbar/NavBar.vue'
import scroll from 'components/common/scroll/Scroll.vue'
import BackTop from 'components/content/backTop/BackTop.vue'
import TabControl from 'components/content/tabcontrol/TabControl.vue'
import GoodsList from 'components/content/goods/GoodsList.vue'
import HomeSwiper from './childComps/HomeSwiper.vue'
import RecommendView from 'views/home/childComps/RecommendView.vue'
import FeatureView from './childComps/FeatureView.vue'
import { getHomeMultidata, getHomeGoods } from 'network/home'
// import { itemListenerMixin } from 'common/mixin.js'


export default {
  name: "Home",
  components: {
    NavBar,
    TabControl,
    HomeSwiper,
    RecommendView,
    FeatureView,
    GoodsList,
    scroll,
    BackTop,

  },
  // mixins: [itemListenerMixin],
  data () {
    return {
      banners: [],
      dKeywords: [],
      keywordss: [],
      recommends: [],
      goods: {
        'pop': { page: 0, list: [] },
        'new': { page: 0, list: [] },
        'sell': { page: 0, list: [] }
      },
      currentType: 'pop',
      isShowBackTop: false,
      tabOffsetTop: 0,
      isTabFixed: false,
      itemImgListener: null,

    }
  },
  created () {
    this.getHomeMultidata()
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')


  },
  mounted () {
    const refresh = debounce(this.$refs.scroll.refresh, 100)
    this.itemImgListener = () => {
      refresh()
    }
    this.$bus.$on('itemImgLoad', this.itemImgListener)


  },
  computed: {
    showGoods () {
      return this.goods[this.currentType].list
    },
    actived () {
      this.$refs.scroll.refresh()
      this.$refs.scroll.scrollTo(0, this.saveY, 0)
    },
    deactived () {
      this.saveY = this.$refs.scroll.getScrollY()
      this.$bus.off('itemImgLoad', this.itemImgListener)
    },

  },
  methods: {
    tabClick (index) {
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break;
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'

      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    backClick () {
      this.$refs.scroll.scrollTo(0, 0)
    },
    contentScroll (position) {
      this.isShowBackTop = (-position.y) > 1000
      this.isTabFixed = (-position.y) > this.tabOffsetTop
    },
    loadMore () {
      this.getHomeGoods(this.currentType)
      this.$refs.scroll.refresh()
    },
    swiperImageLoad () {
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
    },


    //下面是网络请求的方法
    getHomeMultidata () {
      getHomeMultidata().then(res => {
        this.banners = res.data.banner.list;
        this.dKeywords = res.data.dKeyword.list;
        this.keywordss = res.data.keywords.list;
        this.recommends = res.data.recommend.list;
      })
    },
    getHomeGoods (type) {
      const page = this.goods[type].page + 1
      getHomeGoods(type, page).then(res => {

        this.goods[type].list.push(...res.data.list)
        this.goods[type].page += 1
        this.$refs.scroll.finishPullUp()
      })
    }
  },

}
</script>

<style scoped>
#home {
  /* padding-top: 44px; */
  height: 100vh;
  position: relative;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /* position: fixed;
  left: 0;
  top: 0;
  right: 0;
  z-index: 9; */
}

.content {
  position: absolute;
  top: 44px;
  bottom: 49px;
  overflow: hidden;
}
.tab-control {
  position: relative;
  z-index: 9;
}
</style>